<!doctype html>
<html>
<head>
	<meta charset="utf-8">

</head>
<body>
<div>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.js" integrity="sha512-n/4gHW3atM3QqRcbCn6ewmpxcLAHGaDjpEBu4xZd47N0W2oQ+6q7oc3PXstrJYXcbNU1OHdQ1T7pAP+gi5Yu8g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js" integrity="sha512-3j3VU6WC5rPQB4Ld1jnLV7Kd5xr+cq9avvhwqzbH/taCRNURoeEpoPBK9pDyeukwSxwRPJ8fDgvYXd6SkaZ2TA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
	<input name="component" id="component">
	<textarea style="width:100%;height:300px"  name="config" id="config">
	</textarea>

	<button id="submit">
		获取配置数据
	</button>

	<button id="submits">
		获取默认数据
	</button>

	<script>
		$keyoptions={
			large:'大',
			default:'默认',
			small:'小',
			primary:'主要',
			success:'成功',
			warning:'警告',
			danger:'警告',
			info:'信息',
			button:'普通按钮',
			submit:'提交按钮',
			reset:'重置按钮',
			left:'左边',
			right:'右边',
			top:'上边'
		}


		String.prototype.replaceAll=function(str,replace,ingore){
			ingore = ingore || false;
			var reg;
			if(!ingore){
				reg = new RegExp(str,"g");
			}else{
				reg = new RegExp(str,"gi");
			}
			return this.replace(reg,replace);
		}

		function strToupper(str) {
			var arr= str.split('-');
			for (var i = 1; i<arr.length; i++) {
				arr[i]=arr[i].charAt(0).toUpperCase()+arr[i].substr(1,arr[i].length-1)
			}
			return arr.join('');
		}
		$("#submit").click(()=>{
			$config = $("#config").val()
			let datas = []
			$($config).find("tbody").find("tr").each((index,item)=>{
				$tds = $(item).find("td");
				$key = $tds.eq(0).html().trim();
				if($tds.eq(0).children().length>0){
					$key = $tds.eq(0).children(0).html().trim();
				}
				if($key.indexOf("<span")>=0){
					$key = $key.substring(0,$key.indexOf("<span"))
				}
				$key = strToupper($key.replaceAll('\'','',false).trim());
				let data = {}
				data['type'] = $key
				$label = $tds.eq(1).html().trim();
				$type = $tds.eq(2).html().trim();
				if($tds.eq(2).children().length>0){
					$type = $tds.eq(2).children(0).html().trim();
					$type = $type.replaceAll('\'','',false).trim();
				}
				$value = $tds.eq(3).html().trim();
				if($tds.length==4){
					$value = $type;
				}
				if($key=='labelPosition'){
					debugger
				}
				if($type.indexOf('string')>=0 || $value.indexOf("/")>=0||$value.indexOf("|")>=0){
					if($value.indexOf("/")>=0||$value.indexOf("|")>=0){
						data['json']={
							label: $label,
							type: "select",
							options: []
						}
						$options = []
						if($value.indexOf("/")>=0){
							$options = $value.split("/")
						}else{
							$options = $value.split("|")
						}

						$options.forEach(item=>{
							if(item.trim()&&item.indexOf("<del")<0&&item.indexOf("del>")<0){
								data['json'].options.push({
									value: item.trim()=='default'?'':item.trim(),
									label: $keyoptions[item.trim()]?$keyoptions[item.trim()]:item.trim()
								})
							}
						})
					}else if($type=='boolean'){
						data['json']={
							label: $label,
							type: "switch",
						}
					}else if($type=='number'){
						data['json']={
							label: $label,
							type: "number",
						}
					}else{
						if($key.indexOf("icon")>=0){
							data['json']={
								label: $label,
								type: "icon",
							}
						}else{
							data['json']={
								label: $label,
								type: "input",
							}
						}
					}
				}else if($type=='boolean'){
					data['json']={
						label: $label,
						type: "switch",
					}
				}else if($type=='number'){
					data['json']={
						label: $label,
						type: "number",
					}
				}
				datas.push(data)
			})
			console.log(datas)
			if(!$("#component").val().trim()){
				alert("请先配置组件名称")
			}

			$.cookie('component',$("#component").val())
			localStorage.setItem('config',$("#config").val())
			$.ajax({
				type : "POST",  //提交方式
				url : "https://www.diygw.com/viteapi/login/out",//路径
				data : {
					component : $("#component").val().trim(),
					datas:JSON.stringify(datas)
				},//数据，这里使用的是Json格式进行传输
				success : function(result) {//返回数据根据结果进行相应的处理
					alert("成功")
				}
			});
		})


		$("#submits").click(()=>{
			$config = $("#config").val()
			let datas = []
			$($config).find("tbody").find("tr").each((index,item)=>{
				$tds = $(item).find("td");
				$key = $tds.eq(0).html().trim();
				if($tds.eq(0).children().length>0){
					$key = $tds.eq(0).children(0).html().trim();
				}
				if($key.indexOf("<span")>=0){
					$key = $key.substring(0,$key.indexOf("<span"))
				}
				$key = strToupper($key.replaceAll('\'','',false).trim());

				$label = $tds.eq(1).html().trim();
				$type = $tds.eq(2).html().trim();
				if($tds.eq(2).children().length>0){
					$type = $tds.eq(2).children(0).html().trim();
					$type = $type.replaceAll('\'','',false).trim();
				}
				$value = $tds.eq(3).html().trim();
				if($tds.length==5){
					$value = $tds.eq(4).html().trim();
				}
				if($type=='boolean'){
					datas.push({
						key:$key,
						type:'Boolean',
						default:$value
					})
				}else if($type=='number'){
					datas.push({
						key:$key,
						type:'Number',
						default:$value
					})
				}else{
					datas.push({
						key:$key,
						type:'String',
						default:$value.indexOf("—")>=0?"":$value
					})
				}
			})
			console.log(datas)
			if(!$("#component").val().trim()){
				alert("请先配置组件名称")
			}

			$.cookie('component',$("#component").val())
			localStorage.setItem('config',$("#config").val())
			$.ajax({
				type : "POST",  //提交方式
				url : "https://www.diygw.com/viteapi/login/out",//路径
				data : {
					type:'value',
					component : $("#component").val().trim(),
					datas:JSON.stringify(datas)
				},//数据，这里使用的是Json格式进行传输
				success : function(result) {//返回数据根据结果进行相应的处理
					alert("成功")
				}
			});
		})

		$("#component").val($.cookie('component'))
		$("#config").val(localStorage.getItem('config'))
	</script>
</div>
</body>
</html>